package com.southminority.ethnic.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * MyBatis Plus配置类
 * 
 * @author 
 */
@Configuration
@EnableTransactionManagement
@MapperScan({
    "com.southminority.ethnic.mapper.organization",
    "com.southminority.ethnic.mapper.news", 
    "com.southminority.ethnic.mapper.document",
    "com.southminority.ethnic.mapper.user",
    "com.southminority.ethnic.mapper.language",
    "com.southminority.ethnic.mapper.research",
    "com.southminority.ethnic.mapper.contact",
    "com.southminority.ethnic.mapper",
    "com.southminority.ethnic.mapper.otherMapper"
})
public class MyBatisPlusConfig {

    /**
     * MyBatis Plus拦截器
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        
        // 分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        
        // 乐观锁插件
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        
        return interceptor;
    }
    
    /**
     * MyBatis Plus全局配置
     */
    @Bean
    public GlobalConfig globalConfig() {
        GlobalConfig globalConfig = new GlobalConfig();
        GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
        
        // 设置字段策略：NOT_NULL表示非null才会参与sql
        dbConfig.setInsertStrategy(FieldStrategy.NOT_NULL);
        dbConfig.setUpdateStrategy(FieldStrategy.NOT_NULL);
        dbConfig.setWhereStrategy(FieldStrategy.NOT_NULL);
        
        globalConfig.setDbConfig(dbConfig);
        return globalConfig;
    }
}
